<template>
  <div v-show="$route.name === 'apply-check'">
    <!-- 面包屑导航区域 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item :to="{ path: '/apply-list' }">申请记录
    </el-breadcrumb-item>
      <el-breadcrumb-item>申请记录审核</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片视图区域--订单数据修改 -->
    <el-card>
      <div style="margin-bottom: 20px;">申请记录审核</div>
      <el-form :model="saveForm"
        ref="saveFormRef"
        label-width="110px">
        <el-row>
          <el-col :span="8">
            <el-form-item label="采购单号" prop="sell_number" style="width: 300px;">
              <el-input v-model="saveForm.sell_number" readonly></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="采购时间" prop="purchase_time" style="width: 300px;">
              <el-date-picker type="date" placeholder="选择日期" v-model="saveForm.purchase_time" readonly>
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="8" v-if="saveForm.status === 1 || saveForm.status === 3">
            <el-form-item label="运营审核日期" prop="operate_time" style="width: 300px;">
              <el-date-picker type="date" placeholder="选择日期" v-model="saveForm.operate_time" readonly>
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="16" v-if="saveForm.status === 1">
            <el-form-item label="审核通过日期" prop="confirm_time" style="width: 300px;">
              <el-date-picker type="date" placeholder="选择日期" v-model="saveForm.confirm_time" readonly>
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>
        <!-- 商品表格 -->
        <el-row>
          <el-card>
            <!-- 商品列表区域 -->
            <el-table :data="saveForm.list" border stripe height="300" v-loading="loading">
              <el-table-column type="index" width="50"></el-table-column>
              <el-table-column label="分类" prop="category_name"></el-table-column>
              <el-table-column label="商品" prop="good_name"></el-table-column>
              <el-table-column label="单位" prop="unit" min-width="80" align="center"></el-table-column>
              <el-table-column label="数量" prop="count" min-width="80" align="center"></el-table-column>
              <el-table-column label="门店余量" prop="rest_count" min-width="80" align="center"></el-table-column>
              <el-table-column label="备注" prop="comment" min-width="180" align="center"></el-table-column>
            </el-table>
          </el-card>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-button type="success" @click="handleCheck(1)" style="margin-top: 10px;">通过</el-button>
            <el-button type="danger" @click="handleCheck(0)">驳回</el-button>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-button  @click="back" style="margin-top: 10px;">返回</el-button>
          </el-col>
        </el-row>
      </el-form>
    </el-card>
  </div>
</template>
<script>
export default {
  data() {
    return {
      id: '',
      loading: false,
      saveForm: {
        list: [],
        sell_number: '',
        purchase_time: ''
      }
    }
  },
  mounted() {
    this.getInnerData()
  },
  methods: {
    getInnerData() {
      if (this.$route.params.id) {
        this.id = this.$route.params.id
        localStorage.setItem('applyId', this.$route.params.id)
      } else {
        this.id = localStorage.getItem('applyId')
      }
      if (this.id) {
        this.getInner(this.id)
      }
    },
    async getInner(id) {
      const { data: result } = await this.$http.get('/store-inner/' + id)
      if (result.status !== 200) {
        this.loading = false
        return this.$message.error({
          message: result.msg,
          dangerouslyUseHTMLString: true
        })
      }
      this.loading = false
      this.saveForm = result.data
    },
    back () {
      localStorage.removeItem('applyId')
      this.$router.push({ 'name': 'apply-list' })
    },
    async handleCheck(check) {
      var params = {}
      if(!check) {
        this.$prompt('请输入驳回信息', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消'
        }).then( async ({ value }) => {
          params = {
            orderId: this.id,
            check: check,
            comment: value
          }
          const { data: result } = await this.$http.put('/store-inner/check', params)
          if (result.status !== 200) {
            return this.$message.error({
              message: result.msg,
              dangerouslyUseHTMLString: true
            })
          }
          this.$message.success('审批成功')
          this.$router.push({ 
            'name': 'apply-list'
          })
        })
      } else {
        params = {
          orderId: this.id,
          check: check
        }
        const { data: result } = await this.$http.put('/store-inner/check', params)
        if (result.status !== 200) {
          return this.$message.error({
            message: result.msg,
            dangerouslyUseHTMLString: true
          })
        }
        this.$message.success('审批成功')
        this.$router.push({ name: 'apply-list' })    
      }
    }
  }
}
</script>